package cn.lanqiao.web.controller;

import cn.lanqiao.common.dto.OrderDTO;
import cn.lanqiao.common.entity.Order;
import cn.lanqiao.common.utile.Result;
import cn.lanqiao.common.vo.FilmVo;
import cn.lanqiao.common.vo.OrderFilmVo;
import cn.lanqiao.system.service.FilmService;
import cn.lanqiao.system.service.OrderDetailService;
import cn.lanqiao.system.service.OrderService;
import cn.lanqiao.system.vo.OrderDetailVo;
import cn.lanqiao.system.vo.OrderVo;
import com.github.pagehelper.PageInfo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService ;


    @RequestMapping("/getPage")
    public Result getOrderAll(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "8") int pageSize){
        try {
            PageInfo<OrderVo> orderVoPageInfo = orderService.getPage(pageNo, pageSize) ;
            return Result.ok(orderVoPageInfo) ;
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("服务器异常") ;
    }

    @RequestMapping("/getInformation")
    public Result getInformation(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "8") int pageSize, String orderCode, String userPhone){
        try {
            PageInfo<OrderVo> orderVoPageInfo = orderService.getPageInformation(pageNo, pageSize, orderCode, userPhone);
            return Result.ok(orderVoPageInfo) ;
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("服务器异常") ;
    }

    @RequestMapping("/updateOrderStatus")
    public Result updateOrderStatus(Integer id, Integer status){
        int r = orderService.updateOrderStatus(id, status) ;
        if (r > 0){
            return Result.ok() ;
        } else {
            return Result.fail("服务器异常") ;
        }
    }


    @GetMapping("/getOrderByUserId")
    public Result getOrderByUserId(Integer userId){
        try {
            List<OrderDetailVo> listUserOrder = orderService.getOrderByUserId(userId);
            return Result.ok(listUserOrder);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    //下单
    @RequestMapping("/create")
    public Result createOrder(@RequestBody OrderDTO orderDTO){
        System.out.println("收到的orderDTO："+orderDTO);
        orderService.createOrder(orderDTO);
        return Result.ok();
    }
    //按场次查询订单详情
    /*
    @RequestMapping("/find")
    public Result find(Integer id){
        try {
            List<Order> list=orderService.findByOrderId(id);
            return Result.ok(list);
        }catch(Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统错误！");
    }
    */

    //按用户id查看订单详情
    @RequestMapping("find")
    public Result find(Integer id){
        try {
            List<OrderFilmVo> list=orderService.find(id);
            return Result.ok(list);
        }catch(Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统错误!");
    }

    @RequestMapping("test")
    public String test() {
        return "ok" ;
    }
}
